package com.lagou.rpc.consumer.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.springframework.stereotype.Component;

import java.util.concurrent.Callable;

@Component
public class ClientHandler  extends SimpleChannelInboundHandler<String>  implements Callable {

    ChannelHandlerContext context;


    String message;

    //服务端的消息
    String responseMsg;

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        context = ctx;
    }

    @Override
    protected synchronized void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {
            responseMsg = msg;
            notify();

    }

    @Override
    public synchronized Object call() throws Exception {
        context.writeAndFlush(message);
        wait();
        return responseMsg;
    }
}
